package com.itic.appbase.applications.sys.resource.controller;

import com.itic.appbase.applications.sys.common.service.InnerCommonService;
import com.itic.appbase.applications.sys.resource.persistence.model.Resource;
import com.itic.appbase.applications.sys.resource.persistence.model.ResourceExample;
import com.itic.appbase.applications.sys.resource.persistence.model.ResourceExample.Criteria;
import com.itic.appbase.applications.sys.resource.persistence.model.ResourceStat;
import com.itic.appbase.applications.sys.resource.service.ResourceService;
import com.itic.appbase.applications.sys.resource.vo.ResourceVo;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.InitConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.ValueHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/sys/resource")
public class ResourceController
        extends BaseController {
    @Autowired
    private ResourceService                 resourceService;
    @Autowired
    private InnerCommonService              innerCommonService;

    public static final Map<String, String> map = new HashMap<String, String>();
    static {
        map.put("type", "T_DM_RESOURCE_TYPE");
        map.put("mark", "T_DM_RESOURCE_MARK");
    }

    /**
     * 主页面
     * 
     * @return
     */
    @RequestMapping(value = "/init")
    public String init() {
        return "sys/resource/resourceMain";
    }

    /**
     * 统计信息页面
     * 
     * @return
     */
    @RequestMapping(value = "/initStat")
    public String initStat() {
        return "sys/resource/resourceStat";
    }

    /**
     * 添加页面
     * 
     * @return
     */
    @RequestMapping(value = "/initAdd")
    public String initAdd(@ModelAttribute("pId") String pId, Model model, @ModelAttribute("level") String level) {
        if (StringHelper.isNotBlank(pId)) {
            Resource r = resourceService.get(pId);
            if (null != r) {
                model.addAttribute("parentName", r.getName());
            } else {
                model.addAttribute("parentName", InitConst.ROOT_PARENTNAME);
            }
        }
        return "sys/resource/resourceAdd";
    }

    /**
     * 编辑页面
     * 
     * @return
     */
    @RequestMapping(value = "/initEdit")
    public String initEdit(String id, Model model) {
        if (StringHelper.isNotBlank(id)) {
            Resource r = resourceService.get(id);
            if (null != r) {
                ResourceVo resourceVo = new ResourceVo();
                ValueHelper.model2vo(r, resourceVo);
                if (null != r.getpId() && !InitConst.P_ID.equals(r.getpId())) {
                    Resource pr = resourceService.get(r.getpId());
                    resourceVo.setParentName(pr.getName());
                }
                model.addAttribute("r", iticJsonMapper.toJson(resourceVo));
            }
        }
        return "sys/resource/resourceEdit";
    }

    /**
     * 查看页面
     * 
     * @return
     */
    @RequestMapping(value = "/initView")
    public String initView(String id, Model model) {
        if (StringHelper.isNotBlank(id)) {
            Resource r = resourceService.get(id);
            if (null != r) {
                r = (Resource) innerCommonService.getNameByCode(r, map);
                ResourceVo resourceVo = new ResourceVo();
                ValueHelper.model2vo(r, resourceVo);
                if (null != r.getpId() && !InitConst.P_ID.equals(r.getpId())) {
                    Resource pr = resourceService.get(r.getpId());
                    resourceVo.setParentName(pr.getName());
                }
                String result = iticJsonMapper.toJson(resourceVo);
                model.addAttribute("r", result);
            }
        }
        return "sys/resource/resourceView";
    }

    /**
     * 添加
     * 
     * @param request
     * @param resource
     * @param dataList
     * @return
     * @throws Throwable
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Map<String, Object> add(Resource resource) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = ResponseCode.SERVER_ERROR.getCode();
        if (null != resource && StringUtils.isNotBlank(resource.getName())) {
            result = resourceService.add(resource);
        } else {
            result = ResponseCode.PARAM_INVALID.getCode();
        }
        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }

    /**
     * 更新
     * 
     * @param resource
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Map<String, Object> edit(Resource resource) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = resourceService.edit(resource);
        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }

    /**
     * 删除
     * 
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = ResponseCode.SERVER_ERROR.getCode();
        if (StringHelper.isNotBlank(id)) {
            result = resourceService.deleteTree(id);
        } else {
            result = ResponseCode.PARAM_INVALID.getCode();
        }
        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }

    /**
     * 资源数据
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Map<String, Object> list() {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ResourceExample example = new ResourceExample();
        example.createCriteria().andIsEffectiveEqualTo(DBConst.TRUE);
        example.setOrderByClause(" ORDER_NO ");
        List<Resource> result = resourceService.list(example);
        resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        resultMap.put(IticConst.DATA, result);
        return resultMap;
    }

    /**
     * 按类别统计信息
     * 
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/stat")
    @ResponseBody
    public Map<String, Object> stat() {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        Resource resource = new Resource();
        resource.setIsEffective(DBConst.TRUE);
        resource.setType(InitConst.TYPE);
        List<ResourceStat> resultList = resourceService.statByType(resource);
        dataMap.put(IticConst.DATA, resultList);
        dataMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
        return dataMap;
    }

    /**
     * 检查权限标识属性是否重复
     * 
     * @param resource
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/isExist")
    @ResponseBody
    public Map<String, Object> isExist(Resource resource) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        String result = ResponseCode.SERVER_ERROR.getCode();
        if (null != resource && StringHelper.isNotBlank(resource.getPermit())) {
            ResourceExample example = new ResourceExample();
            Criteria c = example.createCriteria();
            c.andPermitEqualTo(resource.getPermit()).andIsEffectiveEqualTo(DBConst.TRUE);
            if (StringHelper.isNotBlank(resource.getId())) {
                c.andIdNotEqualTo(resource.getId());
            }
            List<Resource> resources = resourceService.list(example);
            result = resources != null && !resources.isEmpty() ? ResponseCode.EXIST.getCode() : ResponseCode.SUCCESS
                    .getCode();
        } else {
            result = ResponseCode.PARAM_INVALID.getCode();
        }
        dataMap.put(IticConst.RESULT, result);
        return dataMap;
    }

}